package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.vo.SetmealAndCateAndSetmealDishVO;
import com.itheima.vo.SetmealAndCategory;
import com.itheima.vo.SetmealAndSetmealDishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 小帅蓝
 * @Date 2022/6/20 8:47
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealAndSetmealDishVO setmealAndSetmealDishVO) {
        boolean result = setmealService.addSetmeal(setmealAndSetmealDishVO);
        return result ? R.success("新增套餐成功!") : R.error("新增套餐失败！");
    }

    @GetMapping("/page")
    public R<PageBean> queryAll(String name, Integer page, Integer pageSize) {
        List<SetmealAndCategory> list = setmealService.query(name, page, pageSize);
        Integer total = setmealService.getCount(name);
        return R.success(new PageBean(list, total, pageSize, page));
    }

    @DeleteMapping
    public R<String> deleteSetmeal(String ids) {
        boolean result = setmealService.deleteSetmeal(ids);
        return result ? R.success("删除成功") : R.error("套餐正在售卖中，不能删除");
    }

    @GetMapping("/{id}")
    public R<SetmealAndCateAndSetmealDishVO> queryById(@PathVariable String id) {
        SetmealAndCateAndSetmealDishVO setmealAndCateAndSetmealDishVO = setmealService.queryById(id);
        return R.success(setmealAndCateAndSetmealDishVO);
    }

    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealAndCateAndSetmealDishVO setmealAndCateAndSetmealDishVO) {
        boolean result = setmealService.updateSetmeal(setmealAndCateAndSetmealDishVO);
        return result ? R.success("修改成功!") : R.error("修改失败！");
    }

    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam String ids, @PathVariable Integer status) {
        boolean result = setmealService.updateStatus(ids, status);
        return result ? R.success("操作成功") : R.error("操作失败");
    }

}
